package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityGoodsInfoManagerSearch3Binding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.viewmodel.transport.GoodsInfoManagerSearchModel3;
import com.swgk.core.base.di.AppComponent;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import javax.inject.Inject;

public class GoodsInfoManageSearchActivity3 extends AppActivity {

    private ActivityGoodsInfoManagerSearch3Binding binding;
    private TagAdapter historyAdapter;
    private InputMethodManager im;

    @Inject
    GoodsInfoManagerSearchModel3 searchModel;

    public static void start(Context context){
        Intent intent = new Intent(context,GoodsInfoManageSearchActivity3.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

        DaggerTransportComponent.builder().appComponent(appComponent).transportModule(new TransportModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {

        binding = DataBindingUtil.setContentView(this, R.layout.activity_goods_info_manager_search3);
        binding.setViewModel(searchModel);
        binding.ivBack.setOnClickListener(this);
        binding.tvSearch.setOnClickListener(this);

        im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    protected void init() {
        searchModel.getSearchHistoryListData();
    }

    public void getSearchHistoryData(List<String> ls){

        final LayoutInflater mInflater = LayoutInflater.from(GoodsInfoManageSearchActivity3.this);
        historyAdapter = new TagAdapter<String>(ls) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.item_goods_info_manage_search3, binding.searchHistoryList, false);
                TextView tv = layout.findViewById(R.id.name);
                tv.setText(s);
                return layout;
            }
        };

        binding.searchHistoryList.setAdapter(historyAdapter);
        binding.searchHistoryList.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                Log.v("search=",ls.get(position));
                return true;
            }
        });

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(null!=im){
            im.hideSoftInputFromWindow(binding.editSearch.getWindowToken(),0);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_search:
                searchModel.search();
                break;
        }
    }
}
